<html>
<head><meta charset="utf-8"><title>purpose of `hir::Type::walk` · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html">purpose of `hir::Type::walk`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241178786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241178786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241178786">(Jun 02 2021 at 17:05)</a>:</h4>
<p>I'm trying to find all references to type parameters in a given type, to account for them in an assist. It looks like <code>hir::Type::walk</code> skips them in many places. I'm not entirely sure what the purpose of that method is, can someone enlighten me? Is it supposed to skip parts of the type?</p>



<a name="241180793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241180793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241180793">(Jun 02 2021 at 17:21)</a>:</h4>
<p>We don't really seem to have <em>any</em> type visiting infra that also handles lifetimes and consts, hmm</p>



<a name="241181562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241181562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241181562">(Jun 02 2021 at 17:27)</a>:</h4>
<p>Can't you use  the chalk Visitor/Fold traits?</p>



<a name="241182190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241182190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241182190">(Jun 02 2021 at 17:31)</a>:</h4>
<p>probably yeah, are they supposed to be used outside of <code>hir_ty</code>?</p>



<a name="241182292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241182292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241182292">(Jun 02 2021 at 17:32)</a>:</h4>
<p>Probably not, I don't think we expose chalk outside of hir_ty/hir?</p>



<a name="241182353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241182353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241182353">(Jun 02 2021 at 17:32)</a>:</h4>
<p><code>hir_ty</code> also has its own simlified <code>TypeWalk</code> trait</p>



<a name="241182375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241182375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241182375">(Jun 02 2021 at 17:33)</a>:</h4>
<p>The overall design constraint here is that we don't really want to expose the inner strucutre of <code>Type</code> to the IDE</p>



<a name="241292893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241292893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241292893">(Jun 02 2021 at 17:40)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink  [he/him]</span> what assist would that be?</p>



<a name="241292920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241292920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241292920">(Jun 02 2021 at 17:40)</a>:</h4>
<p><code>extract_type_alias</code></p>



<a name="241293613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241293613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241293613">(Jun 02 2021 at 17:46)</a>:</h4>
<p>Hm, why do you need to know the structure of the type for that? Why isn't <code>hir::Type::to_string</code> not enough?</p>



<a name="241294272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241294272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241294272">(Jun 02 2021 at 17:51)</a>:</h4>
<p>because the extracted type might reference type parameters of the containing item</p>



<a name="241294315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241294315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241294315">(Jun 02 2021 at 17:51)</a>:</h4>
<p>and I want to make the type alias generic over those</p>



<a name="241294425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241294425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241294425">(Jun 02 2021 at 17:52)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Bar</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">f</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="cp">$</span><span class="mi">0</span><span class="n">X</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="o">&gt;</span><span class="cp">$</span><span class="mi">0</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>should result in</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">type</span> <span class="nc">Type</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">X</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Bar</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">f</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Type</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="241295148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241295148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241295148">(Jun 02 2021 at 17:57)</a>:</h4>
<p>I think <code>hir::Type::walk</code> is currently only used for hover links, which have ... special requirements</p>



<a name="241295194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241295194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241295194">(Jun 02 2021 at 17:57)</a>:</h4>
<p>it might be better to just expose special-purpose methods for now</p>



<a name="241295311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241295311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241295311">(Jun 02 2021 at 17:58)</a>:</h4>
<p>it's still kind of surprising that it skips parameters though</p>



<a name="241295363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241295363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241295363">(Jun 02 2021 at 17:58)</a>:</h4>
<p>I don't think places that are actually visible in the code should be skipped</p>



<a name="241295528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241295528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241295528">(Jun 02 2021 at 17:59)</a>:</h4>
<p>soo maybe you can actually improve it and use it</p>



<a name="241295748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/purpose%20of%20%60hir%3A%3AType%3A%3Awalk%60/near/241295748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/purpose.20of.20.60hir.3A.3AType.3A.3Awalk.60.html#241295748">(Jun 02 2021 at 18:01)</a>:</h4>
<p>the <code>TypeWalk</code> trait should go away</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>